<?php
require_once 'Zend/Auth.php';
require_once 'Zend/Auth/Adapter/DbTable.php';
require_once 'Zend/Session/Namespace.php';

class UserController extends Zend_Controller_Action
{
	// Members.
	protected $_title;
    protected $_dbcon=array(
    			'host'=>'localhost',
    			'dbname'=>'zend',
    			'username'=>'root',
    			'password'=>'root'
    			);	
	protected $_DB;
	protected $_ns;
	// Methods.
    public function init()
    {
        /* Initialize action controller here */
    	$option=array(
    			"layout"=>"userlayout",
    			"layoutPath"=>APPLICATION_PATH."/layouts/scripts"
    			);
    	Zend_Layout::startMvc($option);
    	
    	$this->_title=Zend_Registry::get('title');
//     	$this->_DB = new Zend_Db_Adapter_Pdo_Mysql($this->_dbcon);
		$this->_DB = Zend_Registry::get('DB');
    	$this->_ns = new Zend_Session_Namespace('UserManager');
    	$this->_ns->setExpirationSeconds(60);
    }

    public function indexAction()
    {
        // action body
        $registry= Zend_Registry::getInstance();
        
        $this->view->assign('title', $registry['title']);
        $this->view->assign('number', implode(',', $registry['number']));
        $this->view->assign('registerURL', $this->getRequest()->getBaseURL().'/user/register');
    }

    
    
    public function registerAction()
    {
    	// action body
    	$this->view->assign('action', 'process');
    }
    
    public function processAction(){
    	$request=$this->getRequest();
    	$data=array(
    				'first_name'=>$request->getParam('first_name'),
    				'last_name'=>$request->getParam('last_name'),
    				'user_name'=>$request->getParam('user_name'),
    				'password'=>md5($request->getParam('password'))
    			);
    	$this->_DB->insert('user', $data);
    	
    	$this->_forward('list');
    }
    
    public function listAction(){
    	// Check Authenticate
    	$auth= Zend_Auth::getInstance();
    	if(!$auth->hasIdentity()){
    		$this->_redirect('/user/login');
    	}
    	$this->view->assign('logoutURL', $this->getRequest()->getBaseURL().'/user/logout');
//     	$this->_DB->setFetchMode(Zend_Db::FETCH_OBJ);
    	$sql="SELECT * FROM `user` ORDER BY user_name ASC";
//     	$sql="select * from 'user' order by user_name asc";
    	$result = $this->_DB->fetchAll($sql);
    	
    	$this->view->assign('data', $result);
    	$this->view->assign('count', count($result));
    	
    }
    
    public function editAction(){
    	$request=$this->getRequest();
    	
    	$sql="SELECT * FROM user WHERE id=".$request->getParam('id');
    	$result=$this->_DB->fetchRow($sql);
    	
    	$this->view->assign('data', $result);
    	$this->view->assign('action', $request->getBaseURL().'/user/update');
    }
    
    public function updateAction(){
    	$request=$this->getRequest();
    	
    	$data=array(
    				'first_name'=>$request->getParam('first_name'),
    				'last_name'=>$request->getParam('last_name'),
    				'user_name'=>$request->getParam('user_name'),
    				'password'=>md5($request->getParam('password'))
    			);
    	$this->_DB->update('user', $data, 'id = '.$request->getParam('id'));
    	
    	$this->_forward('list');
    }
    
    public function deleteAction(){
    	$this->_DB->delete('user','id='.$this->getRequest()->getParam('id'));
    	
    	$this->_forward('list');
    }
    
    public function loginAction(){
    	$ns= new Zend_Session_Namespace('UserLogin');
    	$ns= $this->_ns;
    	// UnLock Session Namespace.
    	if($ns->isLocked()){
    		$ns->unlock();
    	}
    	// Set session value.
    	if(!isset($ns->logincount)){
    		$ns->logincount= 1;
    		// set expiration=60s.
    		$ns->setExpirationSeconds(6, 'logincount');
    	}
    	else{
    		$ns->logincount++;
    	}
    	// echo session value.
    	echo 'Login count: '.$ns->logincount.'<br/>';
    	foreach($ns as $index => $value){
    		echo "$index => $value";
    	}
    	// set action.
    	$this->view->assign('action', 'loginprocess');
    }
    
    public function loginprocessAction(){
    	$request=$this->getRequest();
    	// Init auth
    	$auth=Zend_Auth::getInstance();
    	$authAdap= new Zend_Auth_Adapter_DbTable($this->_DB);
    	$authAdap->setTableName('user');
    	$authAdap->setIdentityColumn('user_name');
    	$authAdap->setCredentialColumn('password');
    	$authAdap->setIdentity($request->getParam('user_name'));
    	$authAdap->setCredential(md5($request->getParam('password')));
    	// Process Auth and keep in session.
    	$result= $auth->authenticate($authAdap);
    	if($result->isValid()){
    		$data= $authAdap->getResultRowObject(null,'password');
    		$auth->getStorage()->write($data);
    		$this->_redirect('/user/list');
    	}
    	else{
    		$this->_redirect('/user/login');
    	}
    	 
    }
    
    public function logoutAction(){
    	$auth= Zend_Auth::getInstance();
    	$auth->clearIdentity();
    	$this->_redirect('/user/login');
    }
}

